/**
 * Gears
 * <p/>
 * User: joakimd
 * Date: 2013-04-08
 * Time: 11:27
 */
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.SampleDataEvent;
import flash.events.UncaughtErrorEvent;
import flash.media.Sound;

import org.dru.masic.trix.Samples;
import org.dru.masic.trix.Sequencer;
import org.dru.masic.trix.Trix;

[SWF(width="640", height="520", scaleMode="noScale", frameRate="60")]
public class TrixTest extends Sprite {
    private var _samples: Samples;
    private var _sequencer: Sequencer;
    private var _trix: Trix;

    public function TrixTest() {
        addChild(Logger.INSTANCE);
        //
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event: Event): void {
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        //
        Logger.INSTANCE.initialize(loaderInfo);
        //
        var sound: Sound = new Sound();
        //
        _samples = new Samples();
        _sequencer = new Sequencer(_samples);
        _trix = new Trix(_sequencer);
        //
        stage.addEventListener(Event.ENTER_FRAME, _trix.onEnterFrame);
        sound.addEventListener(SampleDataEvent.SAMPLE_DATA, _trix.onSampleData);
        //
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        //
        sound.play();
    }

    private var _next: int = 0;

    private function onKeyDown(event: KeyboardEvent): void {
        _next = 1 - _next;
        _sequencer.nextPos = _next;
        Logger.INSTANCE.log("next " + _next);
    }

    private static function onUncaughtError(event: UncaughtErrorEvent): void {
        Logger.INSTANCE.log(event.error);
    }
}
}
